package com.example.libraryapp.activity.bookDetail;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.example.libraryapp.Constant;
import com.example.libraryapp.R;
import com.example.libraryapp.activity.login.LoginActivity;
import com.example.libraryapp.bean.Book;
import com.example.libraryapp.bean.BookDetail;
import com.example.libraryapp.bean.BorrowInfo;
import com.example.libraryapp.bean.User;
import com.example.libraryapp.bean.response.BaseResponse;
import com.example.libraryapp.network.base.BaseActivity;
import com.example.libraryapp.util.GlideUtil;
import com.example.libraryapp.util.LoginUtil;
import com.example.libraryapp.util.ToastUtil;
import com.example.libraryapp.widget.CnToolbar;

/**
 * @author 伍凯铭
 * @since 2020/6/23
 */
public class BookDetailActivity extends BaseActivity<BookDetailContract.View, BookDetailPresenterIml>
        implements View.OnClickListener,BookDetailContract.View {

    private long mBookId;

    private User mNowUser;

    private int mNowMode;

    private ImageView mBookImg;

    private TextView mBookName;

    private TextView mBookAuthor;

    private TextView mBookPublisher;

    private TextView mBookType;

    private TextView mBookLocation;

    private TextView mBookNumber;

    private TextView mReturnBtn;

    private TextView mBorrowBtn;

    //borrow
    private View mBorrowView;

    private TextView mBorrowStart;

    private TextView mBorrowDDL;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBookId = getIntent().getLongExtra(Constant.BOOK_ID,-1);
        if(mBookId == -1) {
            finish();
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        mNowUser = LoginUtil.getNowUser(this);
        showLoading();
        if(mNowUser != null) {
            mPresenter.getBookDetail(mBookId,mNowUser.getUserId());
        }else {
            mPresenter.getBookDetail(mBookId,0);
        }
    }

    @Override
    public void handleError(String error) {
        ToastUtil.showToast(this,"网络错误: "+error);
    }

    @Override
    public void addFavorite(BaseResponse response) {
        ToastUtil.showToast(this,response.getMessage());
        if(response.getCode() == Constant.SUCCESS_CODE) {
            mNowMode = Constant.ADD;
            mToolbar.setRightIcon(R.drawable.ic_collect_true);
        }
    }

    @Override
    public void deleteFavorite(BaseResponse response) {
        ToastUtil.showToast(this,response.getMessage());
        if(response.getCode() == Constant.SUCCESS_CODE) {
            mNowMode = Constant.DELETE;
            mToolbar.setRightIcon(R.drawable.ic_collect_false);
        }
    }

    @Override
    public void handleBorrow(BaseResponse response) {
        ToastUtil.showToast(this,response.getMessage());
        if(response.getCode() == Constant.SUCCESS_CODE) {
            mPresenter.getBookDetail(mBookId,mNowUser.getUserId());
        }
    }

    @Override
    public void handleReturn(BaseResponse response) {
        ToastUtil.showToast(this,response.getMessage());
        if(response.getCode() == Constant.SUCCESS_CODE) {
            mPresenter.getBookDetail(mBookId,mNowUser.getUserId());
        }
    }

    @Override
    public void showBook(BaseResponse<BookDetail> response) {
        //book
        Book book = response.getResult().getBook();
        GlideUtil.loadImage(this,book.getPicture(),mBookImg);
        mBookAuthor.setText(book.getAuthor());
        mBookName.setText(book.getBookName());
        mBookPublisher.setText(book.getPublisher());
        mBookType.setText(String.format(getResources().getString(R.string.type),book.getType().getTypeName()));
        mBookNumber.setText(String.format(getResources().getString(R.string.surplus),book.getNumber()));
        mBookLocation.setText(String.format(getResources().getString(R.string.location),book.getLocation()));
        //collected
        if(response.getResult().isCollected()) {
            mNowMode = Constant.ADD;
            mToolbar.setRightIcon(R.drawable.ic_collect_true);
        }else {
            mNowMode = Constant.DELETE;
            mToolbar.setRightIcon(R.drawable.ic_collect_false);
        }
        //borrow
        BorrowInfo borrowInfo = response.getResult().getBorrowInfo();
        initBorrowView(borrowInfo);
        if(borrowInfo != null) {
            mBorrowBtn.setText(R.string.already_borrow);
            mBorrowBtn.setEnabled(false);
            mReturnBtn.setText(R.string.return_book);
            mReturnBtn.setEnabled(true);
        }else {
            mReturnBtn.setEnabled(false);
            mBorrowBtn.setText(R.string.borrow);
            mBorrowBtn.setEnabled(true);
        }
    }

    private void initBorrowView(BorrowInfo borrowInfo) {
        if(borrowInfo == null) {
            mBorrowView.setVisibility(View.GONE);
            return;
        }
        mBorrowView.setVisibility(View.VISIBLE);
        mBorrowStart.setText(String.format(getResources().getString(R.string.borrow_start),borrowInfo.getBorrowDate()));
        mBorrowDDL.setText(String.format(getResources().getString(R.string.borrow_ddl),borrowInfo.getReturnDeadline()));
    }

    @Override
    protected void initView(View view) {
        mBookImg = view.findViewById(R.id.book_img);
        mBookAuthor = view.findViewById(R.id.book_author);
        mBookLocation = view.findViewById(R.id.book_location);
        mBookName = view.findViewById(R.id.book_name);
        mBookNumber = view.findViewById(R.id.book_number);
        mBookPublisher = view.findViewById(R.id.book_publisher);
        mBookType = view.findViewById(R.id.book_type);
        mBorrowBtn = view.findViewById(R.id.borrow_btn);
        mReturnBtn = view.findViewById(R.id.return_btn);
        mBorrowView = view.findViewById(R.id.borrow_view);
        mBorrowDDL = view.findViewById(R.id.book_borrow_ddl);
        mBorrowStart = view.findViewById(R.id.book_borrow_start);
        mBorrowBtn.setOnClickListener(this);
        mReturnBtn.setOnClickListener(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_book_detail;
    }

    @Override
    protected BookDetailPresenterIml initPresenter() {
        return new BookDetailPresenterIml();
    }

    @Override
    protected void initToolBar(CnToolbar toolbar) {
        toolbar.setTitle(getResources().getString(R.string.detail));
        toolbar.setBackBtnOnClickListener(this);
        toolbar.setRightIcon(R.drawable.ic_collect_false);
        toolbar.setRightBtnOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.back_btn) {
            finish();
            return;
        }
        if(mNowUser == null) {
            ToastUtil.showToast(this,"请先登录");
            Intent intent = new Intent(BookDetailActivity.this, LoginActivity.class);
            startActivity(intent);
        }else {
            switch (v.getId()) {
                case R.id.toolbar_right_btn:
                    doFavorite();
                    break;
                case R.id.return_btn:
                    mPresenter.returnBook(mNowUser.getUserId(),mBookId);
                    break;
                case R.id.borrow_btn:
                    mPresenter.borrowBook(mNowUser.getUserId(),mBookId);
                    break;
            }
        }
    }

    private void doFavorite() {
        if(mNowMode == Constant.ADD) {
            mPresenter.deleteFavorite(mNowUser.getUserId(),mBookId);
        }else {
            mPresenter.addFavorite(mNowUser.getUserId(),mBookId);
        }
    }
}
